/**
 * 
 */
package com.alstudio.ui.module.web;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.DownloadListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.ui.config.WebPageConfig;
import com.loovee.imaohu.R;

/**
 * 通用网页浏览器
 * 
 * @author alonso lee
 * 
 */
public class ALWebViewActivity extends TitleBarActivity {

	private WebView mWebView;

	private WebPageConfig mConfig;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(getApplicationContext(), getClass().getSimpleName());
		initData();
		showWebView();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.webview_layout);
		mWebView = (WebView) findViewById(R.id.webView);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		mConfig = (WebPageConfig) getIntent().getSerializableExtra("config");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mWebView != null) {
			mWebView.removeAllViews();
			mWebView.destroy();
		}
	}

	/**
	 * 显示URL
	 */
	private void showWebView() {
		if (TextUtils.isEmpty(getCenterTextView().getText().toString())) {
			mWebView.setWebChromeClient(new mWebChromeClient());
		}
		mWebView.setWebViewClient(new myWebViewClient());
		// 启用javascript
		mWebView.getSettings().setJavaScriptEnabled(true);
		// 可以缩放
		mWebView.getSettings().setSupportZoom(true);
		mWebView.getSettings().setBuiltInZoomControls(true);
		// 自适应屏幕宽度
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.getSettings().setLoadWithOverviewMode(true);
		// 监听下载
		setWebViewDownloadListener();
		mWebView.loadUrl(mConfig.getUrl(), ALLocalEnv.getLocalEnv()
				.getDefaultWebHeads());
		showLoadingProgress();

	}

	private final String SCHEME_WTAI = "wtai://wp/";
	private final String SCHEME_WTAI_MC = "wtai://wp/mc;";
	private final String SCHEME_WTAI_SD = "wtai://wp/sd;";
	private final String SCHEME_WTAI_AP = "wtai://wp/ap;";

	private class myWebViewClient extends WebViewClient {

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			super.onPageFinished(view, url);
			dismissLoadingProgress();
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (url.startsWith(SCHEME_WTAI)) {
				if (url.startsWith(SCHEME_WTAI_MC)) {
					Intent intent = new Intent(Intent.ACTION_VIEW,
							Uri.parse(WebView.SCHEME_TEL
									+ url.substring(SCHEME_WTAI_MC.length())));
					ALWebViewActivity.this.startActivity(intent);
					return true;
				}
				if (url.startsWith(SCHEME_WTAI_SD)) {
					// TODO: only send when there is active voice connection
					return false;
				}
				if (url.startsWith(SCHEME_WTAI_AP)) {
					// TODO
					return false;
				}
			}
			showLoadingProgress();
			return false;
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			// TODO Auto-generated method stub
			super.onReceivedError(view, errorCode, description, failingUrl);
			dismissLoadingProgress();
		}

	}

	private class mWebChromeClient extends WebChromeClient {
		@Override
		public void onReceivedTitle(WebView view, String title) {
			// TODO Auto-generated method stub
			setCenterTextViewMessage(title);
			super.onReceivedTitle(view, title);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (mWebView.canGoBack()) {
			showLoadingProgress();
			mWebView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void setWebViewDownloadListener() {
		mWebView.setDownloadListener(new DownloadListener() {
			public void onDownloadStart(String url, String userAgent,
					String contentDisposition, String mimetype,
					long contentLength) {
				// 实现下载的代码
				Uri uri = Uri.parse(url);
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
				ALWebViewActivity.this.finish();
			}
		});
	}
}
